import React from "react";
import { pick } from "lodash";
import HelpTrigger from "@/components/HelpTrigger";
import Link from "@/components/Link";
import { Renderer as VisRenderer, Editor as VisEditor, updateVisualizationsSettings } from "@redash/viz/lib";
import { clientConfig } from "@/services/auth";

import countriesDataUrl from "@redash/viz/lib/visualizations/choropleth/maps/countries.geo.json";
import usaDataUrl from "@redash/viz/lib/visualizations/choropleth/maps/usa-albers.geo.json";
import subdivJapanDataUrl from "@redash/viz/lib/visualizations/choropleth/maps/japan.prefectures.geo.json";

import chinaDataUrl from "@redash/viz/lib/visualizations/choropleth/maps/china.geo.json";
import dataUrl110000 from "@redash/viz/lib/visualizations/choropleth/maps/china/110000.geo.json";
import dataUrl120000 from "@redash/viz/lib/visualizations/choropleth/maps/china/120000.geo.json";
import dataUrl130000 from "@redash/viz/lib/visualizations/choropleth/maps/china/130000.geo.json";
import dataUrl140000 from "@redash/viz/lib/visualizations/choropleth/maps/china/140000.geo.json";
import dataUrl150000 from "@redash/viz/lib/visualizations/choropleth/maps/china/150000.geo.json";
import dataUrl210000 from "@redash/viz/lib/visualizations/choropleth/maps/china/210000.geo.json";
import dataUrl220000 from "@redash/viz/lib/visualizations/choropleth/maps/china/220000.geo.json";
import dataUrl230000 from "@redash/viz/lib/visualizations/choropleth/maps/china/230000.geo.json";
import dataUrl310000 from "@redash/viz/lib/visualizations/choropleth/maps/china/310000.geo.json";
import dataUrl320000 from "@redash/viz/lib/visualizations/choropleth/maps/china/320000.geo.json";
import dataUrl330000 from "@redash/viz/lib/visualizations/choropleth/maps/china/330000.geo.json";
import dataUrl340000 from "@redash/viz/lib/visualizations/choropleth/maps/china/340000.geo.json";
import dataUrl350000 from "@redash/viz/lib/visualizations/choropleth/maps/china/350000.geo.json";
import dataUrl360000 from "@redash/viz/lib/visualizations/choropleth/maps/china/360000.geo.json";
import dataUrl370000 from "@redash/viz/lib/visualizations/choropleth/maps/china/370000.geo.json";
import dataUrl410000 from "@redash/viz/lib/visualizations/choropleth/maps/china/410000.geo.json";
import dataUrl420000 from "@redash/viz/lib/visualizations/choropleth/maps/china/420000.geo.json";
import dataUrl430000 from "@redash/viz/lib/visualizations/choropleth/maps/china/430000.geo.json";
import dataUrl440000 from "@redash/viz/lib/visualizations/choropleth/maps/china/440000.geo.json";
import dataUrl450000 from "@redash/viz/lib/visualizations/choropleth/maps/china/450000.geo.json";
import dataUrl460000 from "@redash/viz/lib/visualizations/choropleth/maps/china/460000.geo.json";
import dataUrl500000 from "@redash/viz/lib/visualizations/choropleth/maps/china/500000.geo.json";
import dataUrl510000 from "@redash/viz/lib/visualizations/choropleth/maps/china/510000.geo.json";
import dataUrl520000 from "@redash/viz/lib/visualizations/choropleth/maps/china/520000.geo.json";
import dataUrl530000 from "@redash/viz/lib/visualizations/choropleth/maps/china/530000.geo.json";
import dataUrl540000 from "@redash/viz/lib/visualizations/choropleth/maps/china/540000.geo.json";
import dataUrl610000 from "@redash/viz/lib/visualizations/choropleth/maps/china/610000.geo.json";
import dataUrl620000 from "@redash/viz/lib/visualizations/choropleth/maps/china/620000.geo.json";
import dataUrl630000 from "@redash/viz/lib/visualizations/choropleth/maps/china/630000.geo.json";
import dataUrl640000 from "@redash/viz/lib/visualizations/choropleth/maps/china/640000.geo.json";
import dataUrl650000 from "@redash/viz/lib/visualizations/choropleth/maps/china/650000.geo.json";
import dataUrl710000 from "@redash/viz/lib/visualizations/choropleth/maps/china/710000.geo.json";
import dataUrl810000 from "@redash/viz/lib/visualizations/choropleth/maps/china/810000.geo.json";
import dataUrl820000 from "@redash/viz/lib/visualizations/choropleth/maps/china/820000.geo.json";

function wrapComponentWithSettings(WrappedComponent) {
  return function VisualizationComponent(props) {
    updateVisualizationsSettings({
      HelpTriggerComponent: HelpTrigger,
      LinkComponent: Link,
      choroplethAvailableMaps: {
        countries: {
          name: "全球国家地图",
          url: countriesDataUrl,
          fieldNames: {
            name: "简称",
            name_long: "全称",
            abbrev: "缩写",
            iso_a2: "ISO代码(2字母)",
            iso_a3: "ISO代码(3字母)",
            iso_n3: "ISO代码(3数字)",
          },
        },
        china: {
          name: "中国行政区划图",
          url: chinaDataUrl,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_all_adcode: {
          name: "(100000)中国省级地图(动态选择)",
          url: dataUrl110000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_110000: {
          name: "(110000)北京市行政区划图",
          url: dataUrl110000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_120000: {
          name: "(120000)天津市行政区划图",
          url: dataUrl120000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_130000: {
          name: "(130000)河北省行政区划图",
          url: dataUrl130000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_140000: {
          name: "(140000)山西省行政区划图",
          url: dataUrl140000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_150000: {
          name: "(150000)内蒙古行政区划图",
          url: dataUrl150000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_210000: {
          name: "(210000)辽宁省行政区划图",
          url: dataUrl210000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_220000: {
          name: "(220000)吉林省行政区划图",
          url: dataUrl220000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_230000: {
          name: "(230000)黑龙江省行政区划图",
          url: dataUrl230000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_310000: {
          name: "(310000)上海市行政区划图",
          url: dataUrl310000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_320000: {
          name: "(320000)江苏省行政区划图",
          url: dataUrl320000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_330000: {
          name: "(330000)浙江省行政区划图",
          url: dataUrl330000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_340000: {
          name: "(340000)安徽省行政区划图",
          url: dataUrl340000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_350000: {
          name: "(350000)福建省行政区划图",
          url: dataUrl350000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_360000: {
          name: "(360000)江西省行政区划图",
          url: dataUrl360000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_370000: {
          name: "(370000)山东省行政区划图",
          url: dataUrl370000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_410000: {
          name: "(410000)河南省行政区划图",
          url: dataUrl410000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_420000: {
          name: "(420000)湖北省行政区划图",
          url: dataUrl420000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_430000: {
          name: "(430000)湖南省行政区划图",
          url: dataUrl430000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_440000: {
          name: "(440000)广东省行政区划图",
          url: dataUrl440000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_450000: {
          name: "(450000)广西行政区划图",
          url: dataUrl450000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_460000: {
          name: "(460000)海南省行政区划图",
          url: dataUrl460000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_500000: {
          name: "(500000)重庆市行政区划图",
          url: dataUrl500000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_510000: {
          name: "(510000)四川省行政区划图",
          url: dataUrl510000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_520000: {
          name: "(520000)贵州省行政区划图",
          url: dataUrl520000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_530000: {
          name: "(530000)云南省行政区划图",
          url: dataUrl530000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_540000: {
          name: "(540000)西藏行政区划图",
          url: dataUrl540000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_610000: {
          name: "(610000)陕西省行政区划图",
          url: dataUrl610000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_620000: {
          name: "(620000)甘肃省行政区划图",
          url: dataUrl620000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_630000: {
          name: "(630000)青海省行政区划图",
          url: dataUrl630000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_640000: {
          name: "(640000)宁夏行政区划图",
          url: dataUrl640000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_650000: {
          name: "(650000)新疆行政区划图",
          url: dataUrl650000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_710000: {
          name: "(710000)台湾行政区划图",
          url: dataUrl710000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_810000: {
          name: "(810000)香港行政区划图",
          url: dataUrl810000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        china_820000: {
          name: "(820000)澳门行政区划图",
          url: dataUrl820000,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
        usa: {
          name: "美国行政区划图",
          url: usaDataUrl,
          fieldNames: {
            name: "Name",
            ns_code: "美国ANSI编码(8字母)",
            geoid: "Geographic ID",
            usps_abbrev: "USPS Abbreviation",
            fips_code: "FIPS Code (2-character)",
          },
        },
        subdiv_japan: {
          name: "日本行政区划图",
          url: subdivJapanDataUrl,
          fieldNames: {
            name: "名称",
            name_alt: "Name (alternative)",
            name_local: "Name (local)",
            iso_3166_2: "ISO-3166-2",
            postal: "Postal Code",
            type: "Type",
            type_en: "Type (EN)",
            region: "Region",
            region_code: "Region Code",
          },
        },
        custom: {
          name: "(自定义地图)",
          url: chinaDataUrl,
          fieldNames: {
            name: "简称",
            adcode: "行政区划编码(6字母)",
          },
        },
      },
      ...pick(clientConfig, [
        "dateFormat",
        "dateTimeFormat",
        "integerFormat",
        "floatFormat",
        "booleanValues",
        "tableCellMaxJSONSize",
        "allowCustomJSVisualizations",
        "hidePlotlyModeBar",
      ]),
    });

    return <WrappedComponent {...props} />;
  };
}

export const Renderer = wrapComponentWithSettings(VisRenderer);
export const Editor = wrapComponentWithSettings(VisEditor);
